<?php

namespace App\Http\Controllers;

use App\Content;
use App\Novel;
use App\Vol;
use Illuminate\Http\Request;

class ContentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return view('admin.content.index',['list'=>Content::where('con_nid',$request->id)->paginate(10)]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        return view('admin.content.add',[
        'vols'=>Vol::where('vol_nid',$request->id)->get()
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
        Content::create(array_merge($request->except(['novelstate']),['con_nid'=>$request->id]));
        // Content::where('con_nid',$request->id)->novel->create(['novelstate'=>$request->novelstate]);
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Content  $content
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $content=Content::find($request->id);
        $temp=Content::where('con_nid',$content->novel->id)->get();
        return view('index.content',['content'=>$content,
            'preContent'=>$temp->firstWhere('id','<',$request->id),
            'nextContent'=>$temp->firstWhere('id','>',$request->id)
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Content  $content
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        return view('admin.content.edit',['content'=>Content::findOrFail($request->id),
        'vols'=>Vol::where('vol_nid',Content::findOrFail($request->id)->con_nid)->get()
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Content  $content
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $content=Content::findOrFail($request->id);
        $content->update($request->except(['novelstate']));
        $content->novel->update(['novelstate'=>$request->novelstate]);
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Content  $content
     * @return \Illuminate\Http\Response
     */
    public function destroy(Content $content)
    {
        //
    }
}
